﻿using System.Media;

namespace IDEAMS.utils
{
    public class SHSoundPlayer
    {
        private static int STOP = 0;
        //private static int PLAY = 1;
        private static int LOOPING_PLAY = 2;

        private SoundPlayer sp = new SoundPlayer();
        private int status = STOP;

        private object locker = new object();

        public void Load(string file)
        {
            sp.SoundLocation = file;
            sp.Load();
        }

        /// <summary>
        /// 异步播放音频
        /// </summary>
        public void Play()
        {
            lock (locker)
            {
                //由于是异步播放，所以不知道当前的状态是否是播放中还是播放完成
                sp.Play();
            }

        }

        public void PlayLooping()
        {
            lock (locker)
            {
                if (status == STOP)
                {
                    sp.PlayLooping();
                    status = LOOPING_PLAY;
                }
            }

        }

        public void Stop()
        {
            lock (locker)
            {
                if (status != STOP)
                {
                    sp.Stop();
                    status = STOP;
                }
            }
        }
    }
}
